/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.AliMessageLog;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AliMessageLogRepository;
import me.zhengjie.service.AliMessageLogService;
import me.zhengjie.service.dto.AliMessageLogDto;
import me.zhengjie.service.dto.AliMessageLogQueryCriteria;
import me.zhengjie.service.mapstruct.AliMessageLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
*
* @description 服务实现
* @author 毛晋辉
* @date 2020-09-03
**/
@Service
@RequiredArgsConstructor
public class AliMessageLogServiceImpl implements AliMessageLogService {

    private final AliMessageLogRepository aliMessageLogRepository;
    private final AliMessageLogMapper aliMessageLogMapper;

    @Override
    public Map<String,Object> queryAll(AliMessageLogQueryCriteria criteria, Pageable pageable){
        Page<AliMessageLog> page = aliMessageLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(aliMessageLogMapper::toDto));
    }

    @Override
    public List<AliMessageLogDto> queryAll(AliMessageLogQueryCriteria criteria){
        return aliMessageLogMapper.toDto(aliMessageLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AliMessageLogDto findById(Long id) {
        AliMessageLog aliMessageLog = aliMessageLogRepository.findById(id).orElseGet(AliMessageLog::new);
        ValidationUtil.isNull(aliMessageLog.getId(),"AliMessageLog","id",id);
        return aliMessageLogMapper.toDto(aliMessageLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AliMessageLogDto create(AliMessageLog resources) {
        return aliMessageLogMapper.toDto(aliMessageLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AliMessageLog resources) {
        AliMessageLog aliMessageLog = aliMessageLogRepository.findById(resources.getId()).orElseGet(AliMessageLog::new);
        ValidationUtil.isNull( aliMessageLog.getId(),"AliMessageLog","id",resources.getId());
        aliMessageLog.copy(resources);
        aliMessageLogRepository.save(aliMessageLog);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            aliMessageLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AliMessageLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AliMessageLogDto aliMessageLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("手机号", aliMessageLog.getPhone());
            map.put(" conent",  aliMessageLog.getConent());
            map.put(" code",  aliMessageLog.getCode());
            map.put(" message",  aliMessageLog.getMessage());
            map.put(" bizId",  aliMessageLog.getBizId());
            map.put(" requestId",  aliMessageLog.getRequestId());
            map.put(" status",  aliMessageLog.getStatus());
            map.put(" createTime",  aliMessageLog.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}